/*
 * @Author: leonard
 * @Date: 2024-08-12 09:26:40
 * @LastEditors: leonard leijiwu@cbd-3d.com
 * @LastEditTime: 2024-08-17 14:27:11
 * @FilePath: /driving/cpp/opencv/test/test_camera.cpp
 * @Description:
 *
 * Copyright (c) 2024 by CBD Technology CO., Ltd, All Rights Reserved.
 */
#include <opencv2/opencv.hpp>

int main()
{
    // cv::VideoCapture cap(4); // open default camera

    cv::VideoCapture cap;
    // cap.open(0);
    cap.open("/dev/video0", cv::CAP_V4L2);

    // check camera is opened
    if (!cap.isOpened())
    {
        std::cerr << "Error: Could not open camera" << std::endl;
        return -1;
    }

    // 设置参数
    // cap.set(cv::CAP_PROP_FRAME_WIDTH, 1000);
    // cap.set(cv::CAP_PROP_XI_HEIGHT, 1000);
    // cap.set(cv::CAP_PROP_FPS, 30);

    // cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));
    cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('Y','U','Y','V'));
    // cap.set(cv::CAP_PROP_FRAME_WIDTH, 1280);
    // cap.set(cv::CAP_PROP_FRAME_HEIGHT, 720);
    // cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
    // cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
    // cap.set(cv::CAP_PROP_FPS, 30);

    // 查询参数
    double frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
    double frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
    double frame_fps = cap.get(cv::CAP_PROP_FPS);

    std::cout << "frame_width:" << frame_width << std::endl;
    std::cout << "frame_height:" << frame_height << std::endl;
    std::cout << "frame_fps:" << frame_fps << std::endl;

    // create a window to show vodie
    cv::namedWindow("Camera", cv::WINDOW_AUTOSIZE);

    // calculate FPS
    int frame_count = 0;
    double fps = 0.0;
    cv::TickMeter tick_meter;
    int picture_num = 0;
    int auto_save_pic_num = 0;

    while (true)
    {
        cv::Mat frame;
        // start time
        tick_meter.start();

        // read current frame
        cap >> frame;
        if (frame.empty())
        {
            std::cerr << "Error: Could not read frame" << std::endl;
            break;
        }
        // stop time
        tick_meter.stop();

        // 计算FPS
        frame_count++;
        if (frame_count >= 10)
        { // 每10帧计算一次
            fps = frame_count / tick_meter.getTimeSec();
            tick_meter.reset();
            frame_count = 0;
        }

        // 在帧上显示FPS
        std::string fps_text = "FPS: " + std::to_string(fps);
        cv::putText(frame, fps_text, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255, 0, 0), 2);

        // show
        cv::imshow("Camera", frame);

        // if (cv::waitKey(20) == 32) // 空格建保存图片
        // {
        //     std::string name = std::to_string(picture_num) + ".jpg";
        //     cv::imwrite(name, frame);
        //     picture_num++;
        //     std::cout << "save a picture " << name << std::endl;
        // }

        // 自动保持
        {

            std::string auto_save_name = "auto_" + std::to_string(auto_save_pic_num) + ".jpg";
            cv::imwrite(auto_save_name, frame);
            auto_save_pic_num++;
            std::cout << "auto save a picture " << auto_save_name << std::endl;
        }

        // 退出
        if (cv::waitKey(30) == 27)
        {
            break;
        }
    }

    // 释放摄像头
    cap.release();

    // 销毁窗口
    cv::destroyAllWindows();

    return 0;
}